<template>
	<view class="new_fllow">
		<view class="fllow_form">
			<view><text>*</text>跟进类型</view>
			<picker @change="changeFollow" :value="followIndex"  range-key="name" :range="followList">
				<view class="form_right">
					<view :class="follow_type ? 'valueActive' : '' ">{{follow_type ? follow_type : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>跟进方式</view>
			<picker @change="changeFollowWay" :value="followWayIndex" range-key="name" :range="followWayList">
				<view class="form_right">
					<view :class="followup_waystr ? 'valueActive' : '' ">{{followup_waystr ? followup_waystr : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view style="width: 50%;">下次跟进时间</view>
			<!-- #ifdef MP-WEIXIN || H5 || APP-PLUS-->
			<picker style="width: 50%;" @change="changeTime" mode="multiSelector" :range="dateTimeArray" :value="dateTime">
				<view class="form_right">
					<view :class="next_followup_time ? 'valueActive' : '' ">{{next_followup_time ? next_followup_time : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
			<!-- #endif -->
			<!-- #ifdef MP-ALIPAY -->
			<picker @change="changeAlipayTime" mode="date">
				<view class="form_right">
					<view :class="next_followup_time ? 'valueActive' : '' ">{{next_followup_time ? next_followup_time : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
			<!-- #endif -->
		</view>
		<view class="fllow_form">
			<view style="padding-left: 27rpx;">上传附件</view>
			<view class="form_right" @click="uploadClick">
				<view>选择附件</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>
		<u-action-sheet
			:list="uploadActions" 
			v-model="uploadShow"
			@click="uploadType"
		></u-action-sheet>
		
		<view class="imglist" v-if="imgList.length != 0">
			<view class="imgbox" v-for="(item,index) in imgList" :key="index">
				<image :src="BASE_IMG_URL+'close.png'" class="delimg" @click="delImg(index)" mode="scaleToFill"></image>
				<image :src="item.url" class="selfimg" mode="scaleToFill"></image>
			</view>
		</view>
		<view class="fllow_file_li" v-if="fileList.length != 0">
			<view class="fil_li" v-for="(item,index) in fileList" :key="index">
				<image :src="BASE_IMG_URL+'fujian.png'" class="fileimg" mode="scaleToFill"></image>
				<view class="filename">{{item.name}}</view>
				<image :src="BASE_IMG_URL+'close.png'" class="closeimg" @click="delFile(index)" mode="scaleToFill"></image>
			</view>
		</view>
		<view class="fllow_area">
			<view class="area_head"><text>*</text>跟进内容</view>
			<textarea maxlength="-1" :value="followup_content" :class="followup_content ? 'valueActive' : '' " @input="changeContent" placeholder="勤跟进,多签单" placeholder-style="color:#999" />
		</view> 
		<examine-template :remindPeople="remindPeople" title="提醒谁看" @delExamine="delRemind"></examine-template>
		<view id="btn" class="btn_g bg-blue button-hover round" @click="$noMultipleClicks(saveFollow)">
			保存
		</view>
		<view class="botsec"></view>
		
	</view>
</template>

<script>
	import {
		pageJumps
	} from "./newFllow_page_jumps.js";
	import {
		netSelect,
		netPrevCustomer,
		netLianxiren,
		netAddFollowRecords,
		netUpdateFollowRecords,
		netFollowRecordDetail,
		
		
	} from '@/api/kehu.js'
	import {
		BASE_URL,
		SIGN_NO
	} from '@/util/api.js'
	import {
		dateTimePicker,
		getMonthDay
	} from '@/util/dateTimePicker.js'
	import { netStaffDefaultReviewer } from '@/api/index.js'
	import { BASE_IMG_URL } from '@/util/api.js'
	import { commonF } from '@/common/common'
	export default {
		mixins: [pageJumps,commonF],
		data() {
			return {
				BASE_IMG_URL:BASE_IMG_URL,
				noClick:true,    //防止  重复点击
				checked: true,
				uploadShow: false,
				// #ifdef MP-WEIXIN
				uploadActions: [
					{
						id: 1,
						text: '拍照',
						color: '#03aa96'
					},
					{
						id: 2,
						text: '从相册中选择',
						color: '#03aa96'
					},
					{
						id: 3,
						text: '选择本地文件',
						color: '#03aa96'
					}
				],
				// #endif
				// #ifdef H5
				uploadActions: [
					{
						id: 1,
						text: '拍照',
						color: '#03aa96'
					},
					{
						id: 2,
						text: '从相册中选择',
						color: '#03aa96'
					}
				],
				// #endif
				followList: [123,465], //跟进类型
				followWayList: [123,465], //跟进类型
				followIndex: 1,
				followWayIndex: 1,
				followup_waystr: '',
				
				followup_id: '',
				followup_member_id: '',
				mobile: '',
				followup_order_id: '',
				followup_type: '',
				followup_content: '',
				followup_feedback: '',
				followup_way: '',
				photo_id: '',
				photo_url: '',
				photo2_id: '',
				photo2_url: '',
				photo3_id: '',
				photo3_url: '',
				audio1_id: '',
				audio1_url: '',
				video1_id: '',
				video1_url : '',
				next_followup_time: '', //下次跟进时间
				
				
				
				kehuList: [], //客户名称
				kehu: [],
				kehuIndex: null,
				contactList: [], //联系人
				concatIndex: null,
				statusList: [], //客户状态
				statusIndex: null,
				//任务提醒
				remindList: ['准时提醒', '5分钟前', '15分钟前', '30分钟前', '一个小时前', '两个小时前', '一天前', '两天前', '一周前'], //提醒
				remindIndex: null,

				//上传附件
				imgList: [],
				fileList: [],
				follow_type: '', //跟进类型
				dateTime: null,
				dateTimeArray: null,
				dateArr: null,

				follow_time: '', //跟进时间
				content: '', //跟进内容
				relation_id: '', //客户名称
				relationName: '',
				relation_type: '', //1客户 2联系人 3合同 4线索 5回款
				follow: '', //客户状态
				DefaultRemind:[],  //默认需要提醒的人
				remindPeople: [], //提醒谁看
				remindType:'record',
				is_event: false, //是否创建 跟进任务
				start_time: '', //跟进时间
				title: '', //跟进内容
			}
		},
		onLoad(options) {
			if(options.followup_id) {
				this.followup_id = options.followup_id
			}
			if(options.followup_member_id) {
				this.followup_member_id = options.followup_member_id
			}
			if(options.followup_type) {
				this.followup_type = options.followup_type
			}
			this.initTime()
			//选择 列表
			this.getSelectList()
		},
		onShow() {
			//获取  默认需要提醒的人
			if(this.followup_id) {
				this.getInfo()
			}
			this.getDefaultRemind()
		},
		//监听  页面 卸载
		onUnload() {
			uni.removeStorageSync('remindlist')
		},
		methods: {
			getInfo() {
				netFollowRecordDetail({
					followup_id: this.followup_id
				}).then(res => {
					let info = res.data
					if(info.followup_order){
						this.followup_order_id = info.followup_order.id
					}
					if(info.followup_member){
						this.followup_member_id = info.followup_member.id
					}
					if(info.followup_staff){
						this.followup_staff_id = info.followup_staff.id
					}
					// if(this.info){
					// 	// 获取  评论 列表
					// 	this.getCommont()
					// }
					
				})
			},
			getSelectList() {
				netSelect().then(res => {
					this.followList = res.data['MEMBER_FOLLOWUP_TYPE']
					this.followWayList = res.data['MEMBER_FOLLOWUP_WAY']
					this.statusList = res.data['MEMBER_SALESTATUS_CATEGORY']
					this.followIndex = 0;
					this.followup_type = this.followList[0].id
					this.follow_type = this.followList[0].name
					this.followWayIndex = 1;
					this.followup_way = this.followWayList[1].id
					this.followup_waystr = this.followWayList[1].name
				})
			},
			initTime() {
				// 获取完整的年月日 时分秒，以及默认显示的数组
				let obj = dateTimePicker()
				// 精确到分的处理，将数组的秒去掉
				let lastArray = obj.dateTimeArray.pop();
				let lastTime = obj.dateTime.pop();
				let lastDate = obj.dateArray.pop()

				this.dateTimeArray = obj.dateTimeArray
				this.dateTime = obj.dateTime
				this.dateArr = obj.dateArray
			},
			 
			//跟进类型
			changeFollow(e) {
				this.followIndex = e.detail.value
				this.follow_type = this.followList[e.detail.value].name
				this.followup_type = this.followList[e.detail.value].id
			},
			changeFollowWay(e) {
				this.followWayIndex = e.detail.value
				this.followup_way = this.followWayList[e.detail.value].id
				this.followup_waystr = this.followWayList[e.detail.value].name
			},
			//跟进时间
			changeTime(e) {
				let arr = e.detail.value
				let str = this.handleTime(arr)
				this.next_followup_time = str
			},
			changeAlipayTime(e) {
				this.next_followup_time = e.detail.value
			},
			handleTime(arr) {
				let year = this.dateArr[0][arr[0]]
				let month = this.dateArr[1][arr[1]]
				let day = this.dateArr[2][arr[2]]
				let hour = this.dateArr[3][arr[3]]
				let minu = this.dateArr[4][arr[4]]
				let str = year + '-' + month + '-' + day +' '+hour+':'+minu
				return str
			},
			change() {
				this.checked = !this.checked;
			},
			// 上传附件
			uploadClick() {
				this.uploadShow = true
			},
			onCancel() {
				this.uploadShow = false
			},
			//跟进内容
			changeContent(e) {
				this.followup_content = e.detail.value
			},
			uploadType(index) {
				if (index == 0) {
					//拍照
					uni.chooseImage({
						count: 1,
						sourceType: ['camera'],
						success: (res) => {
							console.log(res)
							let file = res.tempFilePaths[0]
							this.uploadFile(file, 1)
						}
					})
				} else if (index == 1) {
					//相册 选择
					uni.chooseImage({
						count: 1,
						sourceType: ['album'],
						success: (res) => {
							console.log(res)
							let file = res.tempFilePaths[0]
							this.uploadFile(file, 1)
						}
					})
				} else {
					//选择文件
					wx.chooseMessageFile({
						count: 1,
						type: 'file',
						success: (res) => {
							console.log(res)
							let file = res.tempFiles[0]
							this.uploadFile(file, 2)
						},
						fail: (err) => {
							console.log(err)
						}
					})
				}
			},
			//type  上传图片1  或  文件2
			uploadFile(file, type) {
				uni.uploadFile({
					url: BASE_URL + 'common/upload',
					name: 'file',
					header: {
						'Authori-vipadmin': uni.getStorageSync('token'),
					},
					formData: {
						"uploadPath": "FollowupRecords",
					},
					// #ifdef MP-ALIPAY
					fileType:'image',
					// #endif
					filePath: type == 1 ? file : file.path,
					success: (res) => {
						let data = JSON.parse(res.data)
						console.log("-------------21")
						data = data.data
						this.uploadShow = false
						if(data.errorCode == 0) {
							if (type == 1) {
								 
								this.imgList.push({acc_id: data.retObj[0].acc_id, url: data.retObj[0].webUploadUrl})
								
							} else {
								// file.url = data.data
								file = Object.assign({}, file, data.data)
								this.fileList.push(file)
							}
						}else{
							uni.showToast({
								title:'上传失败',
								icon:'none'
							})
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			//删除 图片
			delImg(index) {
				this.imgList.splice(index, 1)
			},
			//删除  文件
			delFile(index) {
				this.fileList.splice(index, 1)
			},
			//选择客户  名称
			changeKehu(e) {
				this.kehuIndex = e.detail.value
				this.relation_id = this.kehu[e.detail.value].id
				this.relationName = this.kehuList[e.detail.value]
			},
			//客户状态
			changeStatus(e) {
				this.statusIndex = e.detail.value
				this.follow = this.statusList[e.detail.value]
			},
			//是否 创建  跟进任务
			changeEvent() {
				this.is_event = !this.is_event
			},
			//跟进时间
			changeFollowTime(e) {
				this.start_time = e.detail.value[0] + '/' + e.detail.value[1] + '/' + e.detail.value[2]
			},
			saveFollow() {
				console.log('保存')
				let {
					imgList,
					fileList,
					
					followup_id,
					followup_member_id,
					followup_order_id,
					followup_type,
					followup_content,
					followup_feedback,
					followup_way,
					next_followup_time,
					remindPeople
				} = this
				 if (!followup_type) {
					uni.showToast({
						title: '请选择跟进类型',
						icon: 'none',
						duration: 2000
					})
					return
				 }
				 if (!followup_way) {
					uni.showToast({
						title: '请选择跟进方式',
						icon: 'none',
						duration: 2000
					})
					return
				 }
				if (!followup_content) {
					uni.showToast({
						title: '请填写跟进内容',
						icon: 'none',
						duration: 2000
					})
					return
				} 
				let files = '' //上传附件
				let arr = []
				imgList.forEach(ele => {
					arr.push(ele.id)
				})
				fileList.forEach(ele => {
					arr.push(ele.id)
				})
				console.log(imgList, fileList, '文件')
				files = arr.join(',')
				let remindsid = '' //提醒谁看
				let remarr = []
				remindPeople.forEach(ele => {
					remarr.push(ele.id)
				})
				remindsid = remarr.join(',')
				let params = {}
				console.log(remindsid, '类型remindsid')
				{
					//联系人
					params = {
						files,
						imgList,
						fileList,
						followup_id,
						followup_member_id,
						followup_order_id,
						followup_type,
						followup_content,
						followup_feedback,
						followup_way,
						next_followup_time,
						remindsid
					}
				}
				
				if(followup_id) {
					params.id = followup_id
					netUpdateFollowRecords(params).then(res => {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 2000)
					})
				} else {
					netAddFollowRecords(params).then(res => {
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 2000)
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.valueActive{
		color:#333;
	}
	.new_fllow {
		margin-top: 40rpx;
		font-size: 28rpx;

		.fllow_form {
			display: flex;
			justify-content: space-between;
			background-color: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			padding: 30rpx 40rpx 30rpx 47rpx;
			font-size: 32rpx;
			color: #999999;
			.form_right {
				display: flex;
				align-items: center;
				color: #999;
				:first-child{
					width:350rpx;
					text-align: right;
				}
				image {
					width: 40rpx;
					height: 40rpx;
					margin: 28rpx 5rpx 0 0;
				}
			}

			text {
				color: #f00;
				margin-left: 10rpx;
			}

			.form_input {
				input {
					height: 100rpx;
					line-height: 100rpx;
					text-align: right;
					margin-right: 10rpx;
				}
			}

			.fllow_button {
				margin-top: 15rpx;
			}
		}

		.fllow_file_li {
			padding: 24rpx 34rpx;
			background: #fff;
			border-bottom: 1rpx solid #EAEAEA;

			.fil_li {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 14rpx;

				.fileimg {
					width: 20rpx;
					height: 20rpx;
				}

				.filename {
					font-size: 24rpx;
					color: #999999;
				}

				.closeimg {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}

		.imglist {
			padding: 24rpx 34rpx;
			background: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex-wrap: wrap;

			.imgbox {
				width: 160rpx;
				height: 160rpx;
				margin-right: 14rpx;
				position: relative;
				margin-bottom: 24rpx;
				border: 1rpx solid #EAEAEA;

				.delimg {
					width: 32rpx;
					height: 32rpx;
					border-radius: 50%;
					position: absolute;
					right: -16rpx;
					top: -16rpx;
					background: rgba(0, 0, 0, 0.5);
					z-index: 2;
				}

				.selfimg {
					width: 160rpx;
					height: 160rpx;
				}
			}
		}

		.fllow_area {
			background-color: #fff;
			padding: 0 55rpx;
			margin-bottom: 30rpx;
			font-size: 32rpx;
			color: #999999;
			padding-bottom: 30rpx;
			.area_head {
				height: 100rpx;
				line-height: 100rpx;

				text {
					color: #f00;
				}
			}

			textarea {
				height: 300rpx;
				padding: 30rpx;
				background: #f8f8f8;
				border-radius: 15rpx;
				margin: 0 auto;
			}
		}
		.follow_task {
			height: 80rpx;
			line-height: 80rpx;
		}
		#btn{
			width: 500rpx;
			height: 88rpx;
			background: #03AA96;
			border-radius: 29rpx;
			font-size: 34rpx;
			color: #fff;
			line-height: 88rpx;
		}
	}
</style>
